package com.dushiguang.findhoney.View.View58;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.ImageView;

/**
 * 仿照58同城的引导页面
 * Created by ex-dushiguang201 on 2015-11-04.
 */
public class MyImageView extends ImageView{

    Context context;
    /**
     * 需要操作的图片
     */
    Bitmap bitmap = null;
    /**
     * 当前图片使用的举证
     */
    Matrix matrix = new Matrix();
    /**
     * 当前模式
     */
    public int mode = Mode.MODE_NONE;
    /**
     * 平移矩阵
     */
    Matrix moveMatrix = new Matrix();
    /**
     * 缩放矩阵
     */
    Matrix scaleMatrix = new Matrix();
    /**
     * 上次移动到的位置
     */
    Point startPoint;
    Point lastPoint;
    /**
     * 当前移动到的位置
     */
    Point curPoint;
    /**
     * 缩放中心点
     */
    Point centerPoint;
    /**
     * 两个手指移动之后的像素
     */
    float beforeLength;
    /**
     * 两个手指移动之后的像素
     */
    float afterLenght;
    /**
     * 最小宽度
     */
    public double minWidth = 0;
    /**
     * 最小高度
     */
    public double minHeight = 0;
    /**
     * 最大宽度
     */
    public double maxWidth = 0;
    /**
     * 最大高度
     */
    public double maxHeight = 0;
    /**
     * 图片宽度
     */
    public double imgWidth = 0;
    /**
     * 图片高度
     */
    public double imgHeight = 0;
    /**
     * view 宽度
     */
    public double viewWidth = 0;
    /**
     * view 高度
     */
    public double ViewHight = 0;


    public float space_dx = 0;
    public float space_dy = 0;
    /**
     * 更新消息
     */
    public static final int MSG_INVALIDATE = 0;
    public Thread thread;
    private OnClickListener onClickeListenner;

    public MyImageView(Context context) {
        super(context);
    }

    public MyImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public void setImageBitmap(Bitmap bm) {
        // TODO Auto-generated method stub
        super.setImageBitmap(bm);

        Log.e("dushiguang","setImageBitmap");
        this.bitmap = bm;
        if (bm != null) {
            imgWidth = bm.getWidth();
            imgHeight = bm.getHeight();
            maxWidth = imgWidth * 2;
            maxHeight = imgHeight * 2;
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        // TODO Auto-generated method stub
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        Log.e("dushiguang", "onMeasure");
    }

    /**
     * 绘制
     */
    @Override
    protected void onDraw(Canvas canvas) {
        // TODO Auto-generated method stub
        //		super.onDraw(canvas);
        Log.e("dushiguang", "onDraw");
        Paint mPaint = new Paint();
        mPaint.setColor(0xff000000);// 画笔为绿色
        canvas.save();
        canvas.drawBitmap(bitmap, matrix, null);
        canvas.drawCircle(50, 50, 40, mPaint);
        canvas.restore();
    }

    /**
     * 模式
     *
     * @author citygis007
     */
    class Mode {
        /**
         * 什么都没有
         */
        public static final int MODE_NONE = 0;
        /**
         * 平移
         */
        public static final int MODE_MOVE = 1;
        /**
         * 缩放
         */
        public static final int MODE_SCALE = 2;
    }

    public class Point {
        public float x = 0;
        public float y = 0;

        public Point() {
            super();
            // TODO Auto-generated constructor stub
        }

        public Point(MotionEvent event) {
            super();
            this.x = event.getX();
            this.y = event.getY();
        }

        public Point(float x, float y) {
            super();
            this.x = x;
            this.y = y;
        }

        /**
         * 取当前点和传入点的中点
         *
         * @param point
         * @return
         */
        public Point getCenter(Point point) {
            return new Point(x + point.x, y + point.y);
        }

        public float getX() {
            return x;
        }

        public void setX(float x) {
            this.x = x;
        }

        public float getY() {
            return y;
        }

        public void setY(float y) {
            this.y = y;
        }

    }
}
